package com.example.administrator.yuejian35.Home.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuejian35.BaseListAdapter;
import com.example.administrator.yuejian35.Config;
import com.example.administrator.yuejian35.Home.HomeModel;
import com.example.administrator.yuejian35.Home.bean.Game;
import com.example.administrator.yuejian35.Home.bean.Master;
import com.example.administrator.yuejian35.Home.bean.Result;
import com.example.administrator.yuejian35.MainActivity;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.button.ShineButton;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Wolfh on 2017/4/29.
 */

public class GameAdapter extends BaseListAdapter {

    private SharedPreferences settings;
    private SharedPreferences.Editor editor;
    private String Uid;

    public GameAdapter(Context context, List list){
        super(context, list);

    }

    @Override
    protected RecyclerView.ViewHolder onCreateVH(ViewGroup parent, LayoutInflater layoutInflater, int viewType) {
        View ItemView = layoutInflater.inflate(R.layout.item_game,parent,false);

        GameViewHolder viewHolder = new GameViewHolder(ItemView);//定义显示部件

        return viewHolder;
    }

    @Override
    protected void onBindVH(RecyclerView.ViewHolder holder, int position, List data) {
        final Game game = (Game) data.get(position);
        MainActivity mainActivity = new MainActivity();
        if (mainActivity!=null){
            Uid = mainActivity.getUserId();
        }


        settings = this.context.getSharedPreferences(Uid, Context.MODE_PRIVATE);

        editor = settings.edit();

        if(holder instanceof  GameViewHolder){

            final GameViewHolder viewHolder = (GameViewHolder) holder;
            final String gid = game.getGid();
            viewHolder.name.setText(game.getGname());
            viewHolder.like.setText(game.getHits());

        if (!game.getPic().equals("")){
            /* if (game.getPic()!=null){*/
                 Picasso.with(context)
                    .load(Config.GameImages_URL+game.getPic())
                    .into(viewHolder.pic);
          }
          else{
              viewHolder.pic.setImageResource(R.drawable.lunbotu);
          }
            viewHolder.hit.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
                  @Override
                  public void onCheckedChanged(View view, boolean checked) {

                      Log.v("id",gid);
                      if(checked==true){
                          HomeModel homeModel = new HomeModel();
                          Call<Result> call = homeModel.game_hit(gid,"true");
                          call.enqueue(new Callback<Result>() {
                              @Override
                              public void onResponse(Call<Result> call, Response<Result> response) {

                                  viewHolder.like.setText(response.body().getResult());

                                  editor.putString("game_hit","true");
                                  editor.putString(gid,gid);
                                  editor.commit();
                              }

                              @Override
                              public void onFailure(Call<Result> call, Throwable t) {

                              }
                          });
                      }
                      else if(checked==false){
                          HomeModel homeModel = new HomeModel();
                          Call<Result> call = homeModel.game_hit(gid,"false");
                          call.enqueue(new Callback<Result>() {
                              @Override
                              public void onResponse(Call<Result> call, Response<Result> response) {
                                  viewHolder.like.setText(response.body().getResult());

                                  editor.putString("game_hit","false");
                                  editor.putString(gid,gid);
                                  editor.commit();
                              }

                              @Override
                              public void onFailure(Call<Result> call, Throwable t) {

                              }
                          });
                      }
                  }
              });

            String bbb = settings.getString("game_hit","-1");
            String gameid = settings.getString(gid,"-1");
            if(bbb.equals("true")&& gameid.equals(gid)){
                viewHolder.hit.setChecked(true);
            }



                    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Uri uri = Uri.parse(game.getLink());
                     Intent intent = new Intent(Intent.ACTION_VIEW, uri);

                    context.startActivity(intent);

                }
            });



        }
    }


}
